/*
 * NGramLocationPanel.java
 *
 * Created on Nov 13, 2013, 2:51:43 PM
 */
package ngram_reader.viewer.settings;

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.io.File;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import main.Main;
import processorIO.FileIO;
import swingExtended.preferences.PreferenceGroup;
import swingExtended.swingControlGroup.SwingControlMap;

/**
 *
 * @author g1wshimu
 */
public class NGramLocationPanel extends javax.swing.JPanel {

    private final PreferenceGroup PREF_GROUP;
    
    /** Creates new form NGramLocationPanel */
    public NGramLocationPanel() {
        initComponents();

        // Get the preferences
        PREF_GROUP = new PreferenceGroup(Preferences.userRoot().node(
                Main.USER_PREF_ROOT_NODE + "/" + this.getClass()));

        // Add all the TextFields to the TextFieldGroup
        PREF_GROUP.add(textField1Gram, "1gram_location", "");
        PREF_GROUP.add(textField2Gram, "2gram_location", "");
        PREF_GROUP.add(textField3Gram, "3gram_location", "");
        PREF_GROUP.add(textField4Gram, "4gram_location", "");
        PREF_GROUP.add(textField5Gram, "5gram_location", "");

        PREF_GROUP.loadPreferences();
    }

    @Override
    public void removeNotify() {
        super.removeNotify();
        PREF_GROUP.savePreferences();
    }    
    
    public String getNGramLocation(int gram) {
        switch (gram) {
            case 1:
                return textField1Gram.getText();
            case 2:
                return textField2Gram.getText();
            case 3:
                return textField3Gram.getText();
            case 41:
                return textField4Gram.getText();
            case 5:
                return textField5Gram.getText();
            default:
                return "";
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        label1Gram = new javax.swing.JLabel();
        textField1Gram = new javax.swing.JTextField();
        buttonBrowse1Gram = new javax.swing.JButton();
        label2Gram = new javax.swing.JLabel();
        textField2Gram = new javax.swing.JTextField();
        buttonBrowse2Gram = new javax.swing.JButton();
        buttonBrowse3Gram = new javax.swing.JButton();
        textField3Gram = new javax.swing.JTextField();
        label3Gram = new javax.swing.JLabel();
        label4Gram = new javax.swing.JLabel();
        textField4Gram = new javax.swing.JTextField();
        buttonBrowse4Gram = new javax.swing.JButton();
        buttonBrowse5Gram = new javax.swing.JButton();
        textField5Gram = new javax.swing.JTextField();
        label5Gram = new javax.swing.JLabel();

        jLabel1.setText("Find the folder where the n-grams are stored");

        label1Gram.setText("1 Gram Folder");

        textField1Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField1GramActionPerformed(evt);
            }
        });

        buttonBrowse1Gram.setText("Browse");
        buttonBrowse1Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowse1GramActionPerformed(evt);
            }
        });

        label2Gram.setText("2 Gram Folder");

        buttonBrowse2Gram.setText("Browse");
        buttonBrowse2Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowse2GramActionPerformed(evt);
            }
        });

        buttonBrowse3Gram.setText("Browse");
        buttonBrowse3Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowse3GramActionPerformed(evt);
            }
        });

        label3Gram.setText("3 Gram Folder");

        label4Gram.setText("4 Gram Folder");

        buttonBrowse4Gram.setText("Browse");
        buttonBrowse4Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowse4GramActionPerformed(evt);
            }
        });

        buttonBrowse5Gram.setText("Browse");
        buttonBrowse5Gram.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBrowse5GramActionPerformed(evt);
            }
        });

        label5Gram.setText("5 Gram Folder");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(66, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label1Gram)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textField1Gram, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonBrowse1Gram)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(label4Gram)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textField4Gram, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(label3Gram)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textField3Gram, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(label2Gram)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textField2Gram, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(label5Gram)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(textField5Gram, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(buttonBrowse2Gram)
                            .addComponent(buttonBrowse3Gram)
                            .addComponent(buttonBrowse4Gram)
                            .addComponent(buttonBrowse5Gram))
                        .addGap(12, 12, 12))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label1Gram)
                    .addComponent(textField1Gram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBrowse1Gram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label2Gram)
                    .addComponent(textField2Gram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBrowse2Gram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label3Gram)
                    .addComponent(textField3Gram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBrowse3Gram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label4Gram)
                    .addComponent(textField4Gram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBrowse4Gram))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label5Gram)
                    .addComponent(textField5Gram, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonBrowse5Gram))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void browse(JTextField textField) {
        String mainDir;
        try {
            mainDir = textField.getText();
        } catch (NullPointerException e) {
            mainDir = FileIO.getMainDirectory();
        }
        final JFileChooser chooser = new JFileChooser(mainDir);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.showOpenDialog(this);
        File file = chooser.getSelectedFile();
        if (file != null) {
            textField.setText(file.getAbsolutePath());
        }
    }

private void textField1GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField1GramActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_textField1GramActionPerformed

private void buttonBrowse1GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowse1GramActionPerformed
    browse(this.textField1Gram);
}//GEN-LAST:event_buttonBrowse1GramActionPerformed

private void buttonBrowse2GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowse2GramActionPerformed
    browse(this.textField2Gram);
}//GEN-LAST:event_buttonBrowse2GramActionPerformed

private void buttonBrowse3GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowse3GramActionPerformed
    browse(this.textField3Gram);
}//GEN-LAST:event_buttonBrowse3GramActionPerformed

private void buttonBrowse4GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowse4GramActionPerformed
    browse(this.textField4Gram);
}//GEN-LAST:event_buttonBrowse4GramActionPerformed

private void buttonBrowse5GramActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBrowse5GramActionPerformed
    browse(this.textField5Gram);
}//GEN-LAST:event_buttonBrowse5GramActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBrowse1Gram;
    private javax.swing.JButton buttonBrowse2Gram;
    private javax.swing.JButton buttonBrowse3Gram;
    private javax.swing.JButton buttonBrowse4Gram;
    private javax.swing.JButton buttonBrowse5Gram;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel label1Gram;
    private javax.swing.JLabel label2Gram;
    private javax.swing.JLabel label3Gram;
    private javax.swing.JLabel label4Gram;
    private javax.swing.JLabel label5Gram;
    private javax.swing.JTextField textField1Gram;
    private javax.swing.JTextField textField2Gram;
    private javax.swing.JTextField textField3Gram;
    private javax.swing.JTextField textField4Gram;
    private javax.swing.JTextField textField5Gram;
    // End of variables declaration//GEN-END:variables
}
